<?php

namespace Home\Controller;

use Utuo\Region;
use Utuo\DistributionShopForeground;

class DisShopDeliverAddressController extends DistributionShopForeground {
	
	public function _init() {
		layout('disshop');
		$this->setModelName("DistributionShop");
		$this->setModelControllerName("DisShop");
	}
	
	// 修改收货地址
	public function modifyDeliverAddressAction() {
		$addressId = I('id', 0);
		$deliverAddress = M("DistributionShopDeliverAddress")->find($addressId);
		if($deliverAddress['uid'] != $this->uid) $this->error("无权限的访问");
		$deliverAddress['provinceOptions'] = $this->getOption(Region::getProvinceList(), $deliverAddress['province'], '==请选择省份==');
		$deliverAddress['cityOptions'] = $this->getOption(Region::getCityListByProvince($deliverAddress['province']), $deliverAddress['city'], '==请选择城市==');
		$deliverAddress['areaOptions'] = $this->getOption(Region::getAreaListByCity($deliverAddress['city']), $deliverAddress['area'], '==请选择地区==');
		$this->assign("deliverAddress", $deliverAddress);
		$this->display("{$this->modelControllerName}/saveDeliverAddress");
	}
	
	// 添加收货地址
	public function addDeliverAddressAction() {
		$buyUrl = I('buyUrl');
		$deliverAddress = array();
		$deliverAddress['provinceOptions'] = $this->getOption(Region::getProvinceList(), '', '==请选择省份==');
		$this->assign("deliverAddress", $deliverAddress);
		$this->assign("buyUrl", $buyUrl);
		$this->display("{$this->modelControllerName}/saveDeliverAddress");
	}
	
	// 添加收货地址提交
	public function saveDeliverAddressPostAction() {
		$m = M("DistributionShopDeliverAddress");
		if ($m->create()) {
			$m->uid = $this->uid;
			if (!empty($m->id)){
				false !== $m->where("uid=%d and id=%d", $this->uid, $m->id)->save() and $this->ajaxSuccess("保存成功");
			}else
				$m->status = 1;
				$id = $m->add();
				if ( false !== $id ) {
					$m->where(array('uid' => $this->uid, 'id' => array('neq', $id)))->save(array('status' => 0));
					$this->ajaxSuccess("添加成功");
				} 
		}
		$this->ajaxError("保存失败");
	}
	
	// 收货地址列表
	public function deliverAddressListAction() {
		$buyUrl = I('buyUrl');
		/* 获得用户默认收货地址 */
		$deliverAddressLists = M("DistributionShopDeliverAddress address")->join("__CN_PROVINCE__ province on address.province = province.code", "LEFT")->join("__CN_CITY__ city on address.city = city.code", "LEFT")->join("__CN_AREA__ area on address.area = area.code", "LEFT")->field("address.*,province.name as provinceName, city.name as cityName, area.name as areaName")->where("uid=%d", $this->uid)->order("status desc")->select();
		$this->assign("deliverAddressLists", $deliverAddressLists);
		$this->assign("buyUrl", $buyUrl);
		$this->display("{$this->modelControllerName}/" . ACTION_NAME);
	}
	
	// 删除收货地址
	public function deleteDeliverAddressAction() {
		$addressId = I("id", 0, "intval");
		false !== M("DistributionShopDeliverAddress")->where("uid=%d", $this->uid)->delete($addressId) and $this->ajaxSuccess("删除成功!");
		$this->ajaxSuccess("删除失败!");
	}
	
	// 更新地址状态
	public function updateAddressStatusAction() {
		$deliverId = I("deliverId", 0);
		$m = M("DistributionShopDeliverAddress");
		if (false !== $m->where("uid=%d and id=%d", $this->uid, $deliverId)->setField("status", 1)) {
			$m->where("uid=%d and id!=%d", $this->uid, $deliverId)->setField("status", 0);
			$this->ajaxSuccess('更新成功!');
		}
		$this->ajaxSuccess($m->getError());
	}
	
	// 获取城市
	public function getCityPostAction() {
		echo $this->getOption(Region::getCityListByProvince(I('code')), '', '==请选择城市==');
	}
	
	// 获取地区
	public function getAreaPostAction() {
		echo $this->getOption(Region::getAreaListByCity(I('code')), '', '==请选择地区==');
	}
}